package com.io;

import org.junit.Test;

import java.awt.datatransfer.FlavorEvent;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * 既可以作为输入又可以作为输出，已channel的形势打开文件，当读取文件部分信息时，速度比较快。
 * <p>
 * 优缺点：
 * RandomAccessFile 在数据越大，性能越差。因为他是数据文件的一个channel，支持读改原数据文件。BufferedReader是读改数据文件的一个在内存的副本。
 * RandomAccessFile忽略了字符编码的处理，加快了处理速度。
 * 若是对数据操作在BufferedReader创立buffer的时候就做完了，RandomAccessFile自然就快了。
 */
public class RandomAccessFileDemo {
    /**
     * 作为输入输出流
     */
    @Test
    public void test01() {
        String readFilePath = "E:\\project\\javastudy\\src\\com\\io\\RandomAccessFileDemo.java";
        String writeFilePath = "E:\\project\\javastudy\\src\\com\\io\\RandomAccessFileDemo.java.out";
        try (
                RandomAccessFile readFile = new RandomAccessFile(readFilePath, "r");
                RandomAccessFile writeFile = new RandomAccessFile(writeFilePath, "rw")
        ) {
            byte[] buffer = new byte[1024];
            int len;
            while ((len = readFile.read(buffer)) != -1) {
                writeFile.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 此时不是替换之前的文件，而是对文件内容进行修改
     */
    @Test
    public void test02() {
        String writeFilePath = "E:\\project\\javastudy\\src\\com\\io\\RandomAccessFileDemo.java.out";
        try (
                RandomAccessFile writeFile = new RandomAccessFile(writeFilePath, "rw")
        ) {
            writeFile.write("sadfasfdasfd".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * RandomAccessFile 与fileInputRead 性能比较
     * 只读取文件中的部分数据 发现两个用时差不多。
     */
    @Test
    public void test03() throws IOException {
        String path = "out.txt";
        File file = new File(path);

        long length = file.length();
        FileInputStream fileInputStream = new FileInputStream(file);


        int a, b;
        Long start, end;


        RandomAccessFile writeFile = new RandomAccessFile(path, "r");
        start = System.nanoTime();
        writeFile.seek(1000);
        writeFile.seek(1001);
        writeFile.seek(1003);
        writeFile.seek(1005);
        writeFile.seek(1007);
        a = writeFile.read();
        writeFile.seek(length-1);
        b = writeFile.read();
        end = System.nanoTime();
        writeFile.close();
        System.out.println("FileInputStream 读取部分数据耗时 " + (end - start) + " " + a + " " + b);


        start = System.nanoTime();
        fileInputStream.skip(1000);
        fileInputStream.skip(1);
        fileInputStream.skip(2);
        fileInputStream.skip(2);
        fileInputStream.skip(2);
        a = fileInputStream.read();
        fileInputStream.skip(length-1000-10);
        b = fileInputStream.read();
        end = System.nanoTime();
        fileInputStream.close();
        System.out.println("FileInputStream 读取部分数据耗时 " + (end - start) + " " + a + " " + b);





    }


}
